package 链表.单向.基础;

class ListNode{
    int val;
     ListNode next;//next指向新的节点

    public ListNode(int val) {
        this.val = val;
    }

}
public class MyLinkList {
    //节点
    public  ListNode head;
    public void createList(){
        //创建节点
        ListNode listNode = new ListNode(15);
        ListNode listNode1 = new ListNode(56);
        ListNode listNode2 = new ListNode(45);
        ListNode listNode3 = new ListNode(88);

        //节点连接
        listNode.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;

        this.head = listNode;
    }
    //打印
    void display(){
        ListNode cur = head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;//cur向后走一步
        }
        System.out.println();
    }
    //获取长度
    int  size(){
        int count = 0;
        ListNode cur = head;
        while (cur!=null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    public boolean contains(int value){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==value){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public void addFirst(int data){
        //代码不能交换
        //必须先绑定信息
        ListNode listNode = new ListNode(data);
        listNode.next = head;
        head = listNode;
    }
    public void addLast(int data){
        ListNode listNode  = new ListNode(data);

        ListNode cur = head;
        if(cur==null){
            head = listNode;
            return ;
        }
        while(cur.next!=null){//关注next的指向,找到最后一个节点
            cur = cur.next;
        }
        cur.next = listNode;

    }

    public void addIndex(int index,int data){
        if(index<0||index>size()){
            System.out.println("位置有问题");
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return ;
        }
        ListNode cur = head;
        int count = 0;
        ListNode listNode = new ListNode(data);
        while(count<index-1){
            cur =cur.next;
            count++;
        }
        //不能互换位置
        listNode.next = cur.next;
        cur.next = listNode;
    }
    void remove(int data){
        if(head ==null){
            return;
        }
        if(head.val ==data){
            head = head.next;
            return;
        }
        //1.找到你要删除的前一个节点
        ListNode cur = head;
        int count = 0;
        while(cur.next!=null){
            if(cur.next.val == data){
                break;
            }
            count++;
            cur= cur.next;
        }
        //找到要删除的节点
        ListNode del = head;
        int delindex = 0;
        while (del!=null){
            if(del.val ==data){
                break;
            }
            delindex++;
            del = del.next;
        }
        //删除操作
        cur.next = del.next;
    }

    void removeAll(int data){
        if(head ==null){
            return;
        }
        ListNode cur = head.next;
        ListNode previous = head;
        while(cur!=null){
            if(cur.val==data){
                previous.next = cur.next;
                cur = cur.next;
            }else{
                previous = cur;//注意，小心写成 previous.next = cur;//错误
                cur = cur.next;
            }
        }
        if(head.val ==data){
            head = head.next;
        }
    }
    void clear(){
        head = null;
    }
}
class Text{
    public static void main(String[] args) {
        MyLinkList myLinkList = new MyLinkList();
        myLinkList.createList();
        myLinkList.display();
        System.out.println(myLinkList.size());
        myLinkList.addFirst(999);
        myLinkList.display();

        //插入的数据显示的时候是倒叙
        MyLinkList myLinkList1 = new MyLinkList();
        myLinkList1.addFirst(1);
        myLinkList1.addFirst(2);
        myLinkList1.addFirst(3);
        myLinkList1.addLast(3);
        myLinkList1.addFirst(4);
        myLinkList1.addLast(999);
        myLinkList1.display();
        //固定位置插入
        myLinkList1.addIndex(2,888);
        myLinkList1.display();
        //删除数据
        myLinkList1.remove(888);
        myLinkList1.display();
        //删除出现的所有固定数据
        myLinkList1.removeAll(3);
        myLinkList1.display();

    }
}
